home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Text3D / Text3DLoad.java.z / Text3DLoad.java
Encoding:
Java Source  |  2003-08-08  |  8.0 KB  |  280 lines

  1. /*
  2.  *    @(#)Text3DLoad.java 1.19 02/10/21 13:56:22
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import com.sun.j3d.loaders.objectfile.*;
  41. import java.applet.Applet;
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import com.sun.j3d.utils.applet.MainFrame;
  45. import com.sun.j3d.utils.universe.*;
  46. import javax.media.j3d.*;
  47. import javax.vecmath.*;
  48. import java.io.*;
  49. import com.sun.j3d.utils.behaviors.vp.*;
  50. import com.sun.j3d.utils.behaviors.keyboard.*;
  51.  
  52. public class Text3DLoad extends Applet implements ActionListener {
  53.  
  54.     private String fontName = "TestFont";
  55.     private String textString = null;
  56.     private double tessellation = 0.0;
  57.  
  58.     private SimpleUniverse u;
  59.  
  60.     private Button button;
  61.     private boolean behaviorsOn = false;
  62.     private OrbitBehavior orbit;
  63.  
  64.     public BranchGroup createSceneGraph() {
  65.     float sl = textString.length();
  66.     // Create the root of the branch graph
  67.     BranchGroup objRoot = new BranchGroup();
  68.  
  69.         // Create a Transformgroup to scale all objects so they
  70.         // appear in the scene.
  71.         TransformGroup objScale = new TransformGroup();
  72.         Transform3D t3d = new Transform3D();
  73.         // Assuming uniform size chars, set scale to fit string in view
  74.     t3d.setScale(1.2/sl);
  75.         objScale.setTransform(t3d);
  76.         objRoot.addChild(objScale);
  77.  
  78.     // Create the transform group node and initialize it to the
  79.     // identity.  Enable the TRANSFORM_WRITE capability so that
  80.     // our behavior code can modify it at runtime.  Add it to the
  81.     // root of the subgraph.
  82.     TransformGroup objTrans = new TransformGroup();
  83.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  84.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  85.     objScale.addChild(objTrans);
  86.  
  87.     Font3D f3d;
  88.     if (tessellation > 0.0) {
  89.         f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
  90.                  tessellation,
  91.                  new FontExtrusion());
  92.     }
  93.     else {
  94.         f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
  95.                  new FontExtrusion());
  96.     }
  97.     Text3D txt = new Text3D(f3d, textString, 
  98.          new Point3f( -sl/2.0f, -1.f, -1.f));
  99.     Shape3D sh = new Shape3D();
  100.     Appearance app = new Appearance();
  101.     Material mm = new Material();
  102.     mm.setLightingEnable(true);
  103.     app.setMaterial(mm);
  104.     sh.setGeometry(txt);
  105.     sh.setAppearance(app);
  106.     objTrans.addChild(sh);
  107.  
  108.     BoundingSphere bounds =
  109.       new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  110.  
  111.         if (false) {
  112.       Transform3D yAxis = new Transform3D();
  113.       Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  114.                       0, 0,
  115.                       4000, 0, 0,
  116.                       0, 0, 0);
  117.  
  118.       RotationInterpolator rotator =
  119.           new RotationInterpolator(rotationAlpha, objTrans, yAxis,
  120.                        0.0f, (float) Math.PI*2.0f);
  121.       rotator.setSchedulingBounds(bounds);
  122.       objTrans.addChild(rotator);
  123.     }
  124.  
  125.         // Set up the background
  126.         Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
  127.         Background bgNode = new Background(bgColor);
  128.         bgNode.setApplicationBounds(bounds);
  129.         objRoot.addChild(bgNode);
  130.  
  131.     // Set up the ambient light
  132.     Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
  133.     AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  134.     ambientLightNode.setInfluencingBounds(bounds);
  135.     objRoot.addChild(ambientLightNode);
  136.     
  137.     // Set up the directional lights
  138.     Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
  139.     Vector3f light1Direction  = new Vector3f(1.0f, 1.0f, 1.0f);
  140.     Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
  141.     Vector3f light2Direction  = new Vector3f(-1.0f, -1.0f, -1.0f);
  142.     
  143.     DirectionalLight light1
  144.         = new DirectionalLight(light1Color, light1Direction);
  145.     light1.setInfluencingBounds(bounds);
  146.     objRoot.addChild(light1);
  147.     
  148.     DirectionalLight light2
  149.         = new DirectionalLight(light2Color, light2Direction);
  150.     light2.setInfluencingBounds(bounds);
  151.     objRoot.addChild(light2);
  152.  
  153.     return objRoot;
  154.     }
  155.  
  156.     private void usage()
  157.     {
  158.       System.out.println(
  159.     "Usage: java Text3DLoad [-f fontname] [-t tessellation] [<text>]");
  160.       System.exit(0);
  161.     } // End of usage
  162.  
  163.     public Text3DLoad() {}
  164.  
  165.     public Text3DLoad(String args[]) {
  166.  
  167.         if (args.length == 0) {
  168. //       usage();
  169.       textString = "Java3D";
  170.     }
  171.     else {
  172.         for (int i = 0 ; i < args.length ; i++) {
  173.         if (args[i].startsWith("-")) {
  174.             if (args[i].equals("-f")) {
  175.             if (i < args.length - 1) {
  176.                 fontName = args[++i];
  177.             }
  178.             else {
  179.                 usage();
  180.             }
  181.             }
  182.             else if (args[i].equals("-t")) {
  183.             if (i < args.length - 1) {
  184.                 tessellation = Double.parseDouble(args[++i]);
  185.             }
  186.             else {
  187.                 usage();
  188.             }
  189.             }
  190.             else {
  191.             System.err.println("Argument '" + args[i] +
  192.                        "' ignored.");
  193.             }
  194.         }
  195.         else {
  196.             textString = args[i];
  197.         }
  198.         }
  199.     }
  200.  
  201.     if (textString == null) {
  202.       usage();
  203.     }
  204.  
  205.     }
  206.  
  207.     public void init() {
  208.  
  209.         if (textString == null) {
  210.         textString = "Java3D";
  211.     }
  212.     setLayout(new BorderLayout());
  213.  
  214.     button = new Button("remove behaviors");
  215.     button.addActionListener(this);
  216.     Panel p = new Panel();
  217.     p.add(button);
  218.     add("South", p);
  219.     
  220.         GraphicsConfiguration config =
  221.            SimpleUniverse.getPreferredConfiguration();
  222.  
  223.         Canvas3D c = new Canvas3D(config);
  224.     add("Center", c);
  225.  
  226.     // Create a simple scene and attach it to the virtual universe
  227.     BranchGroup scene = createSceneGraph();
  228.     
  229.     // create a SimpleUniverse with 4 TransformGroups for the mouse
  230.     // behaviors
  231.     u = new SimpleUniverse(c);
  232.  
  233.     // add the behaviors to the ViewingPlatform
  234.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  235.     
  236.      viewingPlatform.setNominalViewingTransform();
  237.  
  238.     // add orbit behavior to ViewingPlatform
  239.     orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL |
  240.                   OrbitBehavior.STOP_ZOOM);
  241.     BoundingSphere bounds =
  242.         new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  243.     orbit.setSchedulingBounds(bounds);
  244.     viewingPlatform.setViewPlatformBehavior(orbit);
  245.  
  246.     behaviorsOn = true;
  247.  
  248.     
  249.     u.addBranchGraph(scene);
  250.     }
  251.  
  252.   public void destroy() {
  253.       u.cleanup();
  254.   }
  255.  
  256.     public void actionPerformed(ActionEvent e) {
  257.     if (e.getSource() == button) {
  258.         ViewingPlatform v = u.getViewingPlatform();
  259.         if (behaviorsOn) {
  260.         v.setViewPlatformBehavior(null);
  261.         button.setLabel("add behaviors");
  262.         behaviorsOn = false;
  263.         }
  264.         else {
  265.         v.setViewPlatformBehavior(orbit);
  266.         button.setLabel("remove behaviors");
  267.         behaviorsOn = true;
  268.         }
  269.     }
  270.     }
  271.  
  272.     //
  273.     // The following allows Text3DLoad to be run as an application
  274.     // as well as an applet
  275.     //
  276.     public static void main(String[] args) {
  277.     new MainFrame(new Text3DLoad(args), 700, 700);
  278.     }
  279. }
  280.